<template>
	<view>
		<view class="header borderBottom1">
			<view @click="selectd(index)" class="tabs" :class="index == current ? 'selectdTabs' : ''" v-for="(item, index) in tabs" :key="index">{{ item }}</view>
		</view>
		<view class="line"></view>
		<u-empty text="订单为空" mode="order" :show="orderFlag"></u-empty>
		<view class="container" v-show="!orderFlag">
			<view v-show="current == 0" class="order borderBottom10" @click="orderDetailHandle(item, index)" v-for="(item, index) in list" :key="'a' + index">
				<view class="order_header">
					<text>订单：{{ item.ordernumber }}</text>
					<text style="color: #ff4040;">{{ item.statusname }}</text>
				</view>
				<view class="order_content">
					<view>学员:{{ item.realname }}</view>
					<view class="">
						联系电话:{{item.phone}}
					</view>
					<view>科目:{{ item.subjectstitle }}</view>
					<view>预约时间:{{ item.yuyuedate }} {{ item.yuyuetime == 'am' ? '上午' : '下午' }} {{item.starthour+'点-'+item.endhour+'点'}}</view>
					<view>练习时长:{{ item.yuyueclasshour }}</view>
				</view>
				<view class="order_footer">
					<text>{{ item.createtime }}</text>
					<view>
						<text>￥</text>
						<text>{{ item.money }}</text>
					</view>
				</view>
				<view class="order_state" v-if="item.overtimestatus == 1 || item.hexiaostatus == 1 || item.studylaststatus == 1">
					<view @click.stop="overTime(item)" class="order_state_refund" v-if="item.overtimestatus == 1">结束计时</view>
					<view @click.stop="hexiaoHandle(item)" class="order_state_pay" v-if="item.hexiaostatus == 1">
						<image style="width: 33rpx;height: 33rpx;" src="../../static/img/teacher/center/sweepCode.png" mode=""></image>
						去核销
					</view>
					<view @click.stop="studentAlready(item)" class="order_state_pay" v-if="item.studylaststatus == 1">学员未到</view>
				</view>
			</view>
			<view v-show="current == 1" class="order borderBottom10" @click="orderDetailHandle(item, index)" v-for="(item, index) in list" :key="'b' + index">
				<view class="order_header">
					<text>订单：{{ item.ordernumber }}</text>
					<text style="color: #ff4040;">{{ item.statusname }}</text>
				</view>
				<view class="order_content">
					<view>学员:{{ item.realname }}</view>
					<view class="">
						联系电话:{{item.phone}}
					</view>
					<view>科目:{{ item.subjectstitle }}</view>
					<view>预约时间:{{ item.yuyuedate }} {{ item.yuyuetime == 'am' ? '上午' : '下午' }} {{item.starthour+'点-'+item.endhour+'点'}}</view>
					<view>练习时长:{{ item.yuyueclasshour }}</view>
				</view>
				<view class="order_footer">
					<text>{{ item.createtime }}</text>
					<view>
						<text>￥</text>
						<text>{{ item.money }}</text>
					</view>
				</view>
				<view class="order_state" v-if="item.overtimestatus == 1 || item.hexiaostatus == 1 || item.studylaststatus == 1">
					<view @click.stop="overTime(item)" class="order_state_refund" v-if="item.overtimestatus == 1">结束计时</view>
					<view @click.stop="hexiaoHandle(item)" class="order_state_pay" v-if="item.hexiaostatus == 1">
						<image style="width: 33rpx;height: 33rpx;" v-if="item.hexiaostatus == 1" src="../../static/img/teacher/center/sweepCode.png" mode=""></image>
						去核销
					</view>
					<view @click.stop="studentAlready(item)" class="order_state_pay" v-if="item.studylaststatus == 1">学员未到</view>
				</view>
			</view>
			<view v-show="current == 2" class="order borderBottom10" @click="orderDetailHandle(item, index)" v-for="(item, index) in list" :key="'c' + index">
				<view class="order_header">
					<text>订单：{{ item.ordernumber }}</text>
					<text style="color: #ff4040;">{{ item.statusname }}</text>
				</view>
				<view class="order_content">
					<view>学员:{{ item.realname }}</view>
					<view class="">
						联系电话:{{item.phone}}
					</view>
					<view>科目:{{ item.subjectstitle }}</view>
					<view>预约时间:{{ item.yuyuedate }} {{ item.yuyuetime == 'am' ? '上午' : '下午' }} {{item.starthour+'点-'+item.endhour+'点'}}</view>
					<view>练习时长:{{ item.yuyueclasshour }}</view>
				</view>
				<view class="order_footer">
					<text>{{ item.createtime }}</text>
					<view>
						<text>￥</text>
						<text>{{ item.money }}</text>
					</view>
				</view>
				<view class="order_state" v-if="item.overtimestatus == 1 || item.hexiaostatus == 1 || item.studylaststatus == 1">
					<view @click.stop="overTime(item)" class="order_state_refund" v-if="item.overtimestatus == 1">结束计时</view>
					<view @click.stop="hexiaoHandle(item)" class="order_state_pay" v-if="item.hexiaostatus == 1">
						<image style="width: 33rpx;height: 33rpx;" v-if="item.hexiaostatus == 1" src="../../static/img/teacher/center/sweepCode.png" mode=""></image>
						去核销
					</view>
					<view @click.stop="studentAlready(item)" class="order_state_pay" v-if="item.studylaststatus == 1">学员未到</view>
				</view>
			</view>
			<view v-show="current == 3" class="order borderBottom10" @click="orderDetailHandle(item, index)" v-for="(item, index) in list" :key="'d' + index">
				<view class="order_header">
					<text>订单：{{ item.ordernumber }}</text>
					<text style="color: #ff4040;">{{ item.statusname }}</text>
				</view>
				<view class="order_content">
					<view>学员:{{ item.realname }}</view>
					<view class="">
						联系电话:{{item.phone}}
					</view>
					<view>科目:{{ item.subjectstitle }}</view>
					<view>预约时间:{{ item.yuyuedate }} {{ item.yuyuetime == 'am' ? '上午' : '下午' }} {{item.starthour+'点-'+item.endhour+'点'}}</view>
					<view>练习时长:{{ item.yuyueclasshour }}</view>
				</view>
				<view class="order_footer">
					<text>{{ item.createtime }}</text>
					<view>
						<text>￥</text>
						<text>{{ item.money }}</text>
					</view>
				</view>
				<view class="order_state" v-if="item.overtimestatus == 1 || item.hexiaostatus == 1 || item.studylaststatus == 1">
					<view @click.stop="overTime(item)" class="order_state_refund" v-if="item.overtimestatus == 1">结束计时</view>
					<view @click.stop="hexiaoHandle(item)" class="order_state_pay" v-if="item.hexiaostatus == 1">
						<image style="width: 33rpx;height: 33rpx;" v-if="item.hexiaostatus == 1" src="../../static/img/teacher/center/sweepCode.png" mode=""></image>
						去核销
					</view>
					<view @click.stop="studentAlready(item)" class="order_state_pay" v-if="item.studylaststatus == 1">学员未到</view>
				</view>
			</view>
		</view>
		<view v-show="loadingShow" class="loading">{{loadingText}}</view>
	</view>
</template>
<script>
import { orderList, hexiao, endtiming, studyweidao, QRcode } from '@/api/teacher/order/testOrder.js';
export default {
	data() {
		return {
			current: 0,
			orderFlag: true,
			tabs: [
				'全部订单', //0
				'待服务', //1
				'已服务', //2
				'未服务' //3
			],
			list: [
				// {
				// 	order: 45655645565,
				// 	name: '张三',
				// 	lesson: '科目二',
				// 	time: '2019-10-28  上午',
				// 	roundNumer: 1,
				// 	date: '2019-05-24  14:30:12',
				// 	state: 1,
				// 	price: 60.0
				// },
			],
			page:1,
			loadingText: '加载中...', //加载状态
			loadingShow:true,
		};
	},
	watch: {
		list(val) {
			if (!this.list.length) {
				this.orderFlag = true;
			} else {
				this.orderFlag = false;
			}
		}
	},
	async onLoad(options) {
		if (options.status) {
			this.current = options.status;
			let res = await orderList(this.current);
			this.getOrderList(res)
		} else {
			let res = await orderList();
			this.getOrderList(res)
		}
	},
	onReachBottom() {
		this.loadMore()
	},
	methods: {
		async loadMore(){
			let res = await orderList(this.current,++this.page);
			this.getOrderList(res)
		},
		getOrderList(res){
			if (res.data.data.length == 0) {
				this.loadingText = "没有数据了！";
			} else if (res.data.data.length < 10) {
				this.loadingText = "没有数据了！";
			} else {
				this.loadingText = "加载中";
			}
			if (this.page == 1) {
				this.list = res.data.data;
			} else {
				this.list = this.list.concat(res.data.data)
			}
			if(this.list.length>0){
				this.loadingShow=true
			}else{
				this.loadingShow=false
			}
		},
		async selectd(index) {
			this.page=1;
			uni.showLoading({
				title: '加载中'
			});
			let res = await orderList(index);
			this.getOrderList(res)
			uni.hideLoading();
			this.current = index;
		},
		overTime(item) {
			uni.showModal({
				title: '练车计时',
				mask:true,
				content: '是否结束计时',
				success: async res => {
					if(res.confirm==true){
						let data = await endtiming(item.ordernumber);
						if(data.code==1){
							let listRes = await orderList(this.current);
							this.list = listRes.data;
							uni.showToast({
								title: '结束计时成功！'
							});
						}else{
							let listRes = await orderList(this.current);
							this.list = listRes.data.data;
							uni.showToast({
								title: data.msg,
								icon:'none'
							});
						}
					}		
				},
				fail: () => {}
			});
		},
		async hexiaoHandle(item) {
			var that = this;
			let res = await QRcode();
			if (res.msg == 'success') {
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。

					//                                debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。

					appId: res.data.wxaccessdata.appId, // 必填，公众号的唯一标识

					timestamp: res.data.wxaccessdata.timestamp, // 必填，生成签名的时间戳

					nonceStr: res.data.wxaccessdata.nonceStr, // 必填，生成签名的随机串

					signature: res.data.wxaccessdata.signature, // 必填，签名，见附录1

					jsApiList: ['checkJsApi', 'scanQRCode'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				wx.error(function(res) {
					console.log(res);
				});
				wx.ready(function() {
					wx.scanQRCode({
						needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，

						scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有

						success: async function(res) {
							let data = res.resultStr;
							let num = data.slice(53, -5);
							let hexiaoRes = await hexiao(num);
							if (hexiaoRes.code == 1) {
								uni.showToast({
									title: '核销成功！'
								});
								let listRes = await orderList(that.current);
								that.list = listRes.data.data;
							}else{
								uni.showToast({
									title: '核销失败！请重试',
									icon:'none'
								});
							}
						},
						fail: err => {
							console.log(err);
						}
					});
				});
			}
		},
		orderDetailHandle(item, index) {
			uni.navigateTo({
				url: '/pages/teacher/teacherDetail?type=2&id=' + item.id
			});
		},
		studentAlready(item) {
			uni.showModal({
				title: '学员是否已到场',
				content: '请确认学员到场',
				cancelText: '未到',
				confirmText: '已到',
				success: async res => {
					if (res.cancel) {
						let data = await studyweidao(item.ordernumber);
						if (data.code == 1) {
							let res = await orderList(this.current);
							this.list = res.data.data;
							uni.showToast({
								title: '更改状态成功！'
							});
						}
					}
				},
				fail: err => {}
			});
		}
	}
};
</script>

<style lang="scss">
@import './css/teacherOrder.scss';
</style>
